var request = require('request');
var schedule = require("node-schedule");
var mysql = require('./mydb.js');
var fs = require('fs')

url = 'https://www.zhihu.com/api/v4/members/zhang-jia-wei/followers?include=data%5B*%5D.answer_count%2Carticles_count%2Cgender%2Cfollower_count%2Cis_followed%2Cis_following%2Cbadge%5B%3F(type%3Dbest_answerer)%5D.topics&limit=20&offset=',
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36',
    'Referer': 'https://www.zhihu.com/',
    'Host': 'www.zhihu.com',
    'Upgrade-Insecure-Requests': '1',
    'Cookie':'q_c1=4b5fc21581e444318dd269127c421bcf|1515402388000|1515402388000; _zap=f9289d33-60ea-48f3-916e-d978ac85cdf6; __utmz=155987696.1516001728.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); capsion_ticket="2|1:0|10:1516003874|14:capsion_ticket|44:YmI4NjdmMWQ0NjQ4NDkxMzliYWE4NDZjNTc1YzM5OTQ=|516edcc97bbcced4301a59c301c1411b9275446db1e5e98c4f7e9b588db5161d"; z_c0="2|1:0|10:1516003882|4:z_c0|92:Mi4xeWpGRkJ3QUFBQUFBWUdMSjUtNzlEQ1lBQUFCZ0FsVk5LckJKV3dCZjFOR2NLc2xvWFF0YUFjYkQ0LWRRU0NzUXZn|2a6b86670a301a717da013f9fdb1d7329bd90547f3b684ac244792b680e74e44"; __utma=155987696.1308425753.1516001728.1516086422.1516089926.3; aliyungf_tc=AQAAAEomaWEFhwgAdTRM2iQ78lWiUR0q; d_c0="AADmchbP_wyPTjiA5HMKXPDYxcuQCTxB4L0=|1516105477"; _xsrf=35d718a4-de88-4330-b41a-cea937293e3b; anc_cap_id=832299c8e5e14d95a671efb9140ac0f6'
}


request({url:url+20,headers:headers}, function(error, response, body) {
	console.log(url)
    if (!error && response.statusCode == 200) {
    	var data = JSON.parse(body)
    	// console.log(data)
        var count = data.paging.totals

        var rule1     = new schedule.RecurrenceRule();  
		var times1    = [];
		var save = 480480;
		for(var i=1;i<count-save;i++){
			if(i%2==0){
				times1.push(i)
			}
		}

		var countArray = [];
        for (var i = save;i<count-save;i++){
        	if(i%20==0){
        		countArray.push(i)
        	}
        }
        var dat = data.data;
        for(i in data.data){
        		saveperson(dat[i])
        }

		rule1.second  = times1;  
		var d = 0;
		schedule.scheduleJob(rule1, function(){
		  	fs.appendFile('./log.txt', d+"page:"+countArray[d], (err) => {
			  	if (err) throw err;
			});
		  	d++
		  	getname(countArray[d])
		});
        
    }
});

function getname(page){
	request({url:url+page,headers:headers}, function(error, response, body) {
		
	    if (!error && response.statusCode == 200) {
	    	var data = JSON.parse(body)
	        var dat = data.data;
	        for(i in data.data){
	        	saveperson(dat[i])
	        }
	    }
	});
}



function saveperson(dat){
	var name = dat.name;
	var headline           = dat.headline;
	var answer_count       = dat.answer_count;
	var follower_count     = dat.follower_count;
	var avatar_url         = dat.avatar_url;
	var adressid           = dat.id;
	var url_tokenadresstab = dat.url_token;
	
	var sqlselect = "SELECT name from zhihuperson where name = '"+name+"'"

	mysql.query("SELECT url_tokenadresstab from zhihuperson where url_tokenadresstab = '"+url_tokenadresstab+"'", function(err, rows, fields){  
			    if (err) {  
			        console.log(err);
			        return;
			    }
			    if(rows.length<1){
			    	var sql = "INSERT INTO zhihuperson (name, headline, answer_count, follower_count, avatar_url , adressid, url_tokenadresstab) \
					VALUES ('"+name+"', '"+headline+"', '"+answer_count+"', '"+follower_count+"', '"+avatar_url+"', '"+adressid+"', '"+url_tokenadresstab+"')";
					mysql.query(sql, function(err, rows, fields){
						if (err) {  
							console.log(err);
							return;
						}
						console.log(rows.insertId);
					});
			    }else{
			    	console.log(name+"新增失败");
			    }
			});
}